package com.siruier.boss.ui.base

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import by.kirich1409.viewbindingdelegate.viewBinding
import com.siruier.boss.ui.helper.ILoa
import com.siruier.boss.ui.helper.IMsa
import com.siruier.boss.ui.helper.loa
import com.siruier.boss.ui.helper.msa
import com.siruier.boss.ui.utils.Logger
import java.lang.reflect.ParameterizedType

/**
 * Created by vicky on 2016/11/7.
 */
open class BaseFragment<VB : ViewBinding> : Fragment(), View.OnClickListener, IMsa by msa(), ILoa by loa() {
    protected val vb: VB by viewBinding(::loadViewBinding)
    protected lateinit var mThis: BaseFragment<VB>
    protected var mView: View? = null
    protected var mSavedInstanceState: Bundle? = null
    open val lazy: Boolean get() = false
    private val dispatcher by lazy { requireActivity().onBackPressedDispatcher }
    private val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            onBackPressed()
        }
    }

    private fun loadViewBinding(fragment: Fragment): VB {
        val clz =
            (this::class.java.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<ViewBinding>
        val method = clz.getDeclaredMethod("inflate", LayoutInflater::class.java)
        return method.invoke(null, layoutInflater) as VB
    }

    open fun loadView(load: VB.() -> Unit) = vb.also(load)

    fun onBackPressed() {
        callback.isEnabled = false
        dispatcher.onBackPressed()
    }

    fun hasExtras(): Boolean {
        return arguments != null
    }

    override fun getView(): View? {
        return mView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mThis = this
        initManageStartActivity()
        initLifecycleOwnerActivity()
    }

    open fun loginChange(intent: Intent) {

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = vb.root
        try {
            mSavedInstanceState = savedInstanceState
            if (!lazy) {
                init(mView, savedInstanceState)
                initView(mView, savedInstanceState)
                initData(mView, savedInstanceState)
            }
        } catch (e: Exception) {
            Logger.e(e)
        }
        return mView
    }

    open fun init(rootView: View?, savedInstanceState: Bundle?) {}
    open fun initView(rootView: View?, savedInstanceState: Bundle?) {}
    open fun initData(rootView: View?, savedInstanceState: Bundle?) {}

    override fun onClick(v: View?) {

    }

    fun bindViewClick(vararg views: View) = com.siruier.boss.ui.ext.bindViewClick(this, *views)

}